<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="common.js"></script>
    <style>
        .h1-banner {
            background: #2B6695;
            border-radius: 6px 6px 6px 6px;
            box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5);
            color: #FFFFFF;
            font-family:"微软雅黑", "宋体", "黑体", Arial;
            font-size: 18px;
            font-weight: bold;
            height: 25px;
            line-height: 25px;
            margin: 18px 0 !important;
            padding: 8px 0 5px 5px;
            text-shadow: 2px 2px 3px #222222;
        }

    .timeline-small {
            max-width: 350px;
            max-height: 630px;
            overflow: hidden;
            margin: 30px auto 0;
            box-shadow: 0 0 40px #a0a0a0;
            font-family: 'Open Sans', sans-serif;
        }
        .timeline-small-body ul {
            padding: 1em 0 0 2em;
            margin: 0;
            list-style: none;
            position: relative;
        }
        .timeline-small-body ul::before {
            content: ' ';
            height: 100%;
            width: 5px;
            background-color: #d9d9d9;
            position: absolute;
            top: 0;
            left: 2.4em;
            z-index: -1;
        }
        .timeline-small-body li div {
            display: inline-block;
            margin: 1em 0;
            vertical-align: top;
        }
        .timeline-small-body .bullet {
            width: 1rem;
            height: 1rem;
            box-sizing: border-box;
            border-radius: 50%;
            background: #fff;
            z-index: 1;
            margin-right: 1rem;
            margin-top: 7%;
        }
        .timeline-small-body .bullet.pink {
            background-color: hotpink;
            border: 3px solid #F93B69;
        }
        .timeline-small-body .bullet.green {
            background-color: lightseagreen;
            border: 3px solid #B0E8E2;
        }
        .timeline-small-body .bullet.blue {
            background-color: aquamarine;
            border: 3px solid cadetblue;
        }
        .timeline-small-body .bullet.orange {
            background-color: salmon;
            border: 3px solid #EB8B6E;
        }
        .timeline-small-body .date {
            width: 23%;
            font-size: 0.75em;
            padding-top: 0.40rem;
            padding-right: 2rem;
        }
        .timeline-small-body .desc {
            width: 50%;
        }
        .timeline-small-body h3 {
            font-size: 0.9em;
            font-weight: 400;
            margin: 0;
        }
        .timeline-small-body h4 {
            margin: 0;
            font-size: 0.7em;
            font-weight: 400;
            color: #808080;
        }
    </style>
</head>
<body>
    <h1 class="h1-banner">审批详情</h1>
    <!-- 主要内容部分 -->
    <div id="detail-block" class="timeline-small" style="display: none;">
        <div class="timeline-small-body">
            <ul id="taskHistoryList">
            </ul>
        </div>
    </div>
    <h1 class="h1-banner">审批操作</h1>
        审批意见<input type="text" id="approve-comment"/>
        <input type="button" value="通过" id="approve" onclick="approveLeave(true)"/>
        <input type="button" value="不通过" id="disApprove" onclick="approveLeave(false)"/>

    <script>
        queryHistoryTask();

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if(pair[0] == variable){return pair[1];}
            }
            return '';
        }

        function queryHistoryTask() {
            var userId = getQueryVariable("userId")
            var processInstanceId = getQueryVariable("processInstanceId")
            console.log(userId + "-" + processInstanceId)

            var xmlHttp = new XMLHttpRequest()
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    console.log(xmlHttp.responseText)
                    var response = JSON.parse(xmlHttp.responseText)
                    console.log(response)
                    if (response.data) {
                        var text = ""
                        response.data.forEach(it => {
                            text = text + '<div class="bullet pink"></div>'
                            text = text + '<div class="date">' + formatDate(it.createTime) + '至' + formatDate(it.endTime) + '</div>'
                            text = text + '<div class="desc">'
                            text = text + '<h3>' + it.userName + '</h3>'
                            text = text + '<h4>' + it.comment + '</h4>'
                            var li = document.createElement('li')
                            li.innerHTML = text
                            document.getElementById('taskHistoryList').appendChild(li)
                            document.getElementById('detail-block').style.display = 'block'
                            text = ""
                        });
                    } else alert('查询失败')
                }
            }
            xmlHttp.open("GET", "/api/queryHistoryTask?processInstanceId=" +processInstanceId, true)
            xmlHttp.send();
        }

        function approveLeave(isApprove) {
            var userId = getQueryVariable("userId")
            var processInstanceId = getQueryVariable("processInstanceId")
            var taskId = getQueryVariable("taskId")
            var comment = document.getElementById('approve-comment').value
            console.log(userId + "-" + processInstanceId + '-' + taskId + '-' + comment + '-' + isApprove)

            var xmlHttp = new XMLHttpRequest()
            xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    alert('审批成功')
                    window.location.href = "/index.html"
                    if (response.data) {
                    } else alert('审批失败')
                }
            }
            xmlHttp.open("GET", "/api/executeTask?userId=" +userId + 
            "&taskId=" + taskId + "&comment=" + comment + 
            "&processInstanceId=" + processInstanceId  + "&approve=" + isApprove, true)
            xmlHttp.send();
        }
    </script>
</body>
</html>